const app = getApp()
const connector = require('../../../utils/connector.js')
// onLoad
Page({
  // headline
  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    chooseSize: false,
    maskHidden: false,
    cartNum: 0,
    addCarts: [],
    activity:[],
    shopping:{},
    // ewmtan: "none",
    onloadOronshow: '1',
    

    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    headline:{
      pictures:[
        { img_url: { url: '../../../images/taber/claim.png' } },
        { img_url: { url:'../../../images/taber/claim.png'}}
      ],
      imgs: [
        { img_url: { url: '../../../images/taber/claim.png' } },
        { img_url: { url: '../../../images/taber/claim.png' } }
      ],
      name:"",
      price:"",
      num:0,
      property:[
       
      ]
    },
    currentData: 0,//商品详情/产品参数 切换
    collected: false,//收藏

  },
  //联系客服
  handleContact(e) {
    console.log(e)
    console.log(e.detail.path)
    console.log(e.detail.query)
  },
  // //获取当前滑块的index
  // bindchange: function (e) {
  //   const that = this;
  //   that.setData({
  //     currentData: e.detail.current
  //   })
  // },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;

    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {

      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },

  //商品详情切换
  nChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.index + 1}`,
      icon: 'none'
    });
  },
  /**立即购买 */
  buyShop(e) {
    var that = this
    let headline = that.data.shopping
    let arr = []
    var shopping = {
      id: headline.id,
      // category_id: headline.category_id,
      main_img_url: headline.main_img_url,
      name: headline.name,
      price: headline.price,
      send_type: headline.send_type
    }
    console.log(headline)
    // arr.push(headline)
    arr.push(shopping)
    wx.setStorageSync('headlineBuy', arr)
    wx.setStorageSync('buy_num', 2)
    wx.navigateTo({
      url: '/pages/index/shopCart/accounts/accounts',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    // let widthBL = 750 / wx.getSystemInfoSync().windowWidth
    // let widthtWin = wx.getSystemInfoSync().windowWidth
    // let heightWin = wx.getSystemInfoSync().windowHeight + (100 / widthBL)
    // that.setData({
    //   widthBL: widthBL,
    //   widthtWin: widthtWin,
    //   heightWin: heightWin,
    //   onloadOronshow: '3',
    // })
    // wx.setStorageSync("shopImgER", "")
    // wx.setStorageSync("shopEr", "")
    // const scene = decodeURIComponent(options.scene)
    // if (scene != 'undefined') {
    //   if (scene.indexOf("&p_") > 0) {
    //     var str1 = scene.substring(0, scene.indexOf("&p_"))
    //     var str2 = scene.substring(scene.indexOf("&p_") + 3)
    //     app.poster = str2
    //     app.searchWord = str1
    //   } else {
    //     app.searchWord = scene.slice(0, scene.indexOf("a", 0))
    //     app.goods_id = scene.slice(scene.indexOf("a", 0) + 5)
    //   }
    // }
    // if (options.id) {
    //   app.searchWord = options.id;
    //   app.goods_id = options.shopid
    // }


    var sets;
    var nums = 0;
   
  },
  //收藏
  collect(e){
    let that = this
    let collected = !that.data.collected
    that.setData({
      collected: collected
    })
    userGoods(that, that.data.shopping.id)
    if (collected){
      wx.showToast({
        title:"收藏成功",
        icon:'none'
      })
    }else{
      wx.showToast({
        title: "取消收藏",
        icon: 'none'
      })
    }
  },

  /**添加购物车 */
  addCart: function(e) {
    var shopPan = false;
    var that = this
    let headline = that.data.headline
    let addCarts = that.data.addCarts;
    let cartNum = that.data.cartNum + 1;
    if (addCarts.length < 1) {
      addCarts.push(headline)
    } else {
      for (var i = 0, len = addCarts.length; i < len; i++) {
        if (addCarts[i].id == headline.id) {
          shopPan = true;
          addCarts[i].num = addCarts[i].num + 1
        }
      }
      if (!shopPan) {
        addCarts.push(headline)
      }

    }
    wx.setStorage({
      key: "addCarts",
      data: addCarts
    })
    that.setData({
      cartNum: cartNum
    })

    /**返回上一页 */
    // wx.navigateBack({
    //   delta: 1
    // })
    // that.setData({
    //   addCarts
    // })
  },
  /**跳转购物车 */
  shopCart: function(e) {
    var that = this
    let addCarts = that.data.addCarts;
    wx.setStorageSync('addCarts', addCarts)

    wx.navigateTo({
      url: '/pages/index/shopCart/shopCart',
    })
  },

  //查看图片
  tu(e) {
    var that = this
    var src = []
    src.push(e.currentTarget.dataset.src)
    wx.previewImage({
      // current: '', // 当前显示图片的http链接
      urls: src
    })
  },

  /*跳转个人详情*/
  onself: function(e) {
    wx.navigateTo({
      url: '/pages/index/personage/personage',
    })
  },
  //
  // cang(e) {
  //   this.setData({
  //     maskHidden: false
  //   })
  // },

 


  /**跳到商城首页 */
  shopping: function(e) {
    /**跳到tab页面必须用 wx.switchTab */
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this
    // console.log(app.globalData.shoppingId)
    goodsDetail(that, app.globalData.shoppingId)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    // if (that.data.onloadOronshow == "1") {
    //   shoppingAjax(that)
    //   var phoneNumber;
    //   if (wx.getStorageSync("phoneNumber") && wx.getStorageSync("phoneNumber") != '') {
    //     if (parseInt(wx.getStorageSync("phoneNumber"))) {
    //       phoneNumber = 1
    //     } else {
    //       phoneNumber = 2
    //     }
    //   } else {
    //     phoneNumber = 2
    //   }
    //   wx.getStorage({
    //     key: 'addCarts',
    //     success: function(res) {
    //       let arr = res.data
    //       let cartNum = 0;
    //       for (var j = 0; j < arr.length; j++) {
    //         cartNum = cartNum + arr[j].num
    //       }

    //       that.setData({
    //         addCarts: res.data,
    //         cartNum: cartNum
    //       })
    //     }
    //   })
    //   that.setData({
    //     authorizationGet: wx.getStorageSync('authorizationGet'),
    //     phoneNumber: phoneNumber
    //   })
    // }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
/**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    // let val = app.searchWord
    // console.log(val)
    return {
      title: "欢迎进入题库",
      path: '/pages/first/first?goTo=3&shop='+ app.globalData.shoppingId +'&pid='  + app.globalData.myId,
      success: function (res) {
      },
      fail: function (res) {
        // 分享失败
      }
    }
 
  }
})


//获取商品详情  
function goodsDetail(that, shoppingId) {
  connector.getFunc(connector.connector().goodsDetail + shoppingId, "", "get", app.tokens, function (res) {
    if (res.statusCode == 200) {
      var arr = []
      // console.log(res.data)
      goodsCollect(that, res.data.id)
      that.setData({
        shopping: res.data
      })

    } else {
      wx.showToast({
        title: res.statusCode + ":" + res.errMsg,
        icon: 'none',
        duration: 1000
      })
    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//收藏 
function userGoods(that, shoppingId) {
  const shoppingIds={
    product_id: shoppingId
  }
  connector.getFunc(connector.connector().userGoods, shoppingIds, "post", app.tokens, function (res) {
    if (res.statusCode == 200) {
      // console.log(res.data)
      // console.log(res)

    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}

//判断是否用户收藏 
function goodsCollect(that, shoppingId) {

  connector.getFunc(connector.connector().goodsCollect + shoppingId, "", "get", app.tokens, function (res) {
    if (res.data.result) {
      // console.log("11111111111")
      that.setData({
        collected:true
      })
    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}